﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using System.Data;
public partial class administrator_uc_admin_m_groupmodule : WebPageSecurity
{
    public static DataTable dt = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["groupid"]))
            {
                LoadDataToDropDownListCategory(DropdownGroups);
                LoadDataInfo(Request.QueryString["groupid"].ToString());
                ViewDataGridDS();
                LoadgvDSChonByGroupID(new Guid(hdItemID.Value));
            }
        }
    }
    private void LoadDataToDropDownListCategory(DropDownList cboName)
    {
        DataTable dtb = new CmsBLL().Groups_GetList("","");
        if (dtb.Rows.Count > 0)
        {
            new CommonBLL().LoadDropDonwList(cboName, dtb, "GroupName", "GroupID");
        }
    }
    private void LoadDataInfo(string GroupID)
    {
        hdItemID.Value = GroupID;
        DropdownGroups.SelectedValue = GroupID + "";
    }
    private void ViewDataGridDS()
    {
        DataTable dt = new CmsBLL().Modules_GetList("","");
        grvDanhSach.DataSource = dt;
        grvDanhSach.DataBind();
    }
    private void LoadgvDSChonByGroupID(Guid GroupID)
    {
        grvDanhSachChon.DataSource = null;
        grvDanhSachChon.DataBind();
        DataTable dt = new CmsBLL().GroupModule_Search(GroupID);
        if (dt.Rows.Count > 0)
        {
            grvDanhSachChon.DataSource = dt;
            grvDanhSachChon.DataBind();
        }
    }   
    protected void btnAddModule_Click(object sender, EventArgs e)
    {
        if (grvDanhSach.Rows.Count > 0)
        {
            List<string> listid = new List<string>();
            List<string> retlistid = new List<string>();
            for (int i = 0; i < grvDanhSach.Rows.Count; i++)
            {
                CheckBox chkChon = (CheckBox)grvDanhSach.Rows[i].FindControl("chkCheckgvDS");
                HiddenField hdFieldID = (HiddenField)grvDanhSach.Rows[i].FindControl("hdItemIDgvDS");

                if (chkChon.Checked == true)
                {
                    listid.Add(hdFieldID.Value);
                }
            }
            if (listid.Count == 0)
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('Bạn chưa chọn module nào để thêm');", true);
            else
                for (int i = listid.Count - 1; i >= 0; i--)
                {
                    if (int.Parse("0" + new CmsBLL().GroupModule_CheckValueToInsert(new Guid(hdItemID.Value), listid[i])) > 0)
                    {
                        retlistid.Add(listid[i]);
                    }
                    else
                    {
                        try
                        {
                            new BLL.CmsBLL().GroupModule_InsertItem(new Guid(hdItemID.Value), listid[i]);
                        }
                        catch (Exception)
                        {
                            retlistid.Add(listid[i]);
                        }
                    }
                }
            if (retlistid.Count > 0)
            {
                string strvalue = "Các module không được thêm ở danh sách chọn: ";
                for (int i = 0; i < retlistid.Count; i++)
                {
                    strvalue += "  -" + new BLL.CmsBLL().Modules_GetTitle(retlistid[i]);
                }
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + strvalue + "');", true);
            }
        }
        LoadgvDSChonByGroupID(new Guid(hdItemID.Value));
    }
    protected void btnRemoveModule_Click(object sender, EventArgs e)
    {
        if (grvDanhSachChon.Rows.Count > 0)
        {
            List<string> listid = new List<string>();
            List<string> retlistid = new List<string>();
            for (int i = 0; i < grvDanhSachChon.Rows.Count; i++)
            {
                CheckBox chkChon = (CheckBox)grvDanhSachChon.Rows[i].FindControl("chkCheckgvDSChon");
                HiddenField hdFieldID = (HiddenField)grvDanhSachChon.Rows[i].FindControl("hdItemIDgvDSChon");

                if (chkChon.Checked == true)
                {
                    listid.Add(hdFieldID.Value);
                }
            }
            if (listid.Count == 0)
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('Bạn chưa chọn module nào để xóa');", true);
            else
                for (int i = listid.Count - 1; i >= 0; i--)
                {
                    try
                    {
                        new BLL.CmsBLL().GroupModule_DeleteItem(new Guid(hdItemID.Value), listid[i]);
                    }
                    catch (Exception)
                    {
                        retlistid.Add(listid[i]);
                    }
                }
            if (retlistid.Count > 0)
            {
                string strvalue = "Các items không được xóa khỏi danh sách chọn: ";
                for (int i = 0; i < retlistid.Count; i++)
                {
                    strvalue += "  -" + new BLL.CmsBLL().Modules_GetTitle(retlistid[i]);
                }
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + strvalue + "');", true);
            }
        }
        LoadgvDSChonByGroupID(new Guid(hdItemID.Value));
    }    
    private void LoadDataToDropDownListPermission(DropDownList cboName)
    {
        ListItem list = new ListItem();
        cboName.Items.Clear();
        cboName.Items.Add(list);
        list.Selected = true;
        list.Value = "0";
        list.Text = "---No Permission---";
        string xmlFile = Server.MapPath("" + DAL.EnvInfo.PathConfigPermiss + "ListPermission.xml");
        if (new System.IO.FileInfo(xmlFile).Exists)
        {
            DataSet dsLoad = new DataSet();
            dsLoad.ReadXml(xmlFile);
            DataTable dtb = dsLoad.Tables[0];
            if (dtb.Rows.Count > 0)
            {
                new CommonBLL().LoadDropDonwList(cboName, dtb, "Title", "ID");
            }
        }            
    }
    protected void grvDanhSachChon_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            string ModuleID = e.CommandArgument.ToString();
            GridViewRow row = (GridViewRow)((LinkButton)e.CommandSource).Parent.Parent;
            DropDownList dropdown = (DropDownList)grvDanhSachChon.Rows[row.RowIndex].FindControl("DropDownListPermission");
            new CmsBLL().GroupModule_UpDateItem(new Guid(hdItemID.Value), ModuleID, int.Parse("0" + dropdown.SelectedValue));
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('Cập nhật thành công "+new CmsBLL().Modules_GetTitle(ModuleID)+"');", true);
        }
        catch (Exception ex)
        {
            ScriptManager.RegisterStartupScript(this,this.GetType(),"Alert","alert('Lỗi xử lý: "+ex.Message.ToString()+"');",true);
        }
       

    }
    protected void grvDanhSachChon_RowCreated(object sender, GridViewRowEventArgs e)
    {
        try
        {
            DropDownList dropdown = (DropDownList)e.Row.FindControl("DropDownListPermission");
            LoadDataToDropDownListPermission(dropdown);
        }
        catch (Exception)
        {

        }
    }
    protected void lbtUpdateAll_Click(object sender, EventArgs e)
    {
        int intNumCheck = 0;
        string ModuleUpdateError = string.Empty;
        if (grvDanhSachChon.Rows.Count > 0)
        {
            for (int i = 0; i < grvDanhSachChon.Rows.Count; i++)
            {
                try
                {
                    DropDownList dropdown = (DropDownList)grvDanhSachChon.Rows[i].FindControl("DropDownListPermission");
                    string ModuleID = grvDanhSachChon.DataKeys[i]["ModuleID"].ToString();
                    new CmsBLL().GroupModule_UpDateItem(new Guid(hdItemID.Value), ModuleID, int.Parse("0" + dropdown.SelectedValue));
                }
                catch (Exception)
                {
                    intNumCheck++;
                    ModuleUpdateError += grvDanhSachChon.DataKeys[i]["ModuleID"].ToString() + " - ";
                }                
            }
            if (intNumCheck > 0)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert1", "alert('" + intNumCheck.ToString() + " Module chưa được cập nhật: " + ModuleUpdateError + "');", true);
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert1", "alert('Cập nhật thành công!');", true);
            }
        }
    }
}